/** 权限相关
 * role_id: 1项目经理 2 班组 3 普通工人
 * **/
import Vue from 'vue'
const state = {
  currentUser: {}
}

const getters = {
  currentUser: state => {
    return state.currentUser
  }
}

const mutations = {
  resetCurrUser (state) {
    state.currentUser = {}
  },
  muCurrentUser (state, payload) {
    state.currentUser = payload.res
  },
  editCurrentUser (state, payload) {
    state.currentUser = {...state.currentUser, ...payload}
  }
}

const actions = {
  userInfo ({ commit }, payload) {
    return new Promise((resolve, reject) => {
      const user = Vue.prototype.$getCookie('currentUser')
      if (user) {
        commit({
          type: 'muCurrentUser',
          res: JSON.parse(user)
        })
        resolve(JSON.parse(user))
      } else {
        if (payload && payload.callBack) {
          reject()
        } else {
          Vue.prototype.$message.error('未登录')
          setTimeout(() => {
            location.replace(`${Vue.prototype.$site.passportServer}`)
          }, 1000)
        }
      }
    })
  }
}

export default {
  state,
  getters,
  mutations,
  actions
}
